package com.xxx.video.main.fragments;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;

import com.xxx.video.R;
import com.xxx.video.channel.adapters.ChannelListAdapter;
import com.xxx.video.channel.beans.TypeInfoBean;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.main.activitys.VideoDetailsActivity;
import com.xxx.video.main.beans.VideoListBean;
import com.xxx.video.main.presenters.HomeChannelListPresenter;
import com.xxx.video.main.presenters.IHomeChannelListView;

import java.util.Objects;

import butterknife.BindView;

public class HomeChannelListFragment extends BaseFragment<HomeChannelListPresenter> implements IHomeChannelListView, BaseRecycleViewAdapter.OnItemClickListener<VideoListBean>, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener {

    @BindView(R.id.rcv_home_channel_list)
    RefreshRecyclerView rcvHomeChannelList;
    /**
     * id
     */
    private String mId;
    /**
     * adapter
     */
    private ChannelListAdapter mChannelListAdapter;

    public static HomeChannelListFragment newInstance(Bundle args) {
        HomeChannelListFragment fragment = new HomeChannelListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected HomeChannelListPresenter createPresenter() {
        return new HomeChannelListPresenter(Objects.requireNonNull(getActivity()), this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirst) {
            isFirst = false;
            getPresenter().getTypeInfo(mId);
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_home_channel_list;
    }

    @Override
    public void initData(Bundle extras) {
        if (extras != null) {
            mId = extras.getString("Id");
        }
    }

    @Override
    public void refreshData(Bundle extras) {
        getPresenter().getTypeInfo(mId);
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initData2View(TypeInfoBean successOb) {
        rcvHomeChannelList.resetState();
        if (mChannelListAdapter == null) {
            rcvHomeChannelList.setLayoutManager(new GridLayoutManager(getActivity(), 2));
            mChannelListAdapter = new ChannelListAdapter(Objects.requireNonNull(getActivity()));
            rcvHomeChannelList.setAdapter(mChannelListAdapter);
            mChannelListAdapter.setOnItemClickListener(this);
            rcvHomeChannelList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
        }
        mChannelListAdapter.setData(successOb.applist);
        if (ListUtils.isEmpty(successOb.applist)) {
            showRootLayoutStatus(2);
        } else {
            showRootLayoutStatus(1);
        }
    }

    @Override
    public void startAppDetailsActivity(String videoId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", videoId);
        intent2Activity(VideoDetailsActivity.class, bundle);
    }

    @Override
    public void onItemClick(VideoListBean videoListBean, int position, int type) {
        startAppDetailsActivity(videoListBean.fId);
    }

    @Override
    public void onRecyclerviewRefresh() {
        getPresenter().getTypeInfo(mId);
    }

    @Override
    public void onRecyclerviewLoadMore() {

    }
}
